package com.itheima.web.client;

import com.github.pagehelper.PageInfo;
import com.itheima.model.ResponseData.ArticleResponseData;
import com.itheima.model.ResponseData.StaticticsBo;
import com.itheima.model.domain.Article;
import com.itheima.model.domain.Comment;
import com.itheima.model.domain.Statistic;
import com.itheima.service.IArticleService;
import com.itheima.service.ISiteService;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Slf4j
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private ISiteService siteService;
    @Autowired
    private IArticleService articleService;
    // 管理中心起始页
    @GetMapping(value = {"", "/index"})
    public String index(HttpServletRequest request) {
        // 获取最新的5篇博客、评论以及统计数据
        List<Article> articles = siteService.recentArticles(5);
        List<Comment> comments = siteService.recentComments(5);
        StaticticsBo staticticsBo = siteService.getStatistics();
        // 向Request域中存储数据
        request.setAttribute("comments", comments);
        request.setAttribute("articles", articles);
        request.setAttribute("statistics", staticticsBo);
        return "back/index";

    }

    // 向文章发表页面跳转
    @GetMapping(value = "/article/toEditPage")
    public String newArticle( ) {
        return "back/article_edit";
    }

    // 发表文章
    @PostMapping(value = "/article/publish")
    @ResponseBody
    public ArticleResponseData publishArticle(Article article){
        if (StringUtils.isBlank(article.getCategories())) {
            article.setCategories("默认分类");
        }
        try {
            articleService.publish(article);
            log.info("文章发布成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            log.error("文章发布失败，错误信息: "+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    // 跳转到后台文章列表页面
    @GetMapping(value = "/article")
    public String index(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "count", defaultValue = "10") int count,
                        HttpServletRequest request){
        PageInfo<Article> articlePageInfo = articleService.selectArticleWithPage(page, count);
        request.setAttribute("articles",articlePageInfo);
        return "back/article_list";
    }

    // 向文章修改页面跳转
    @GetMapping(value = "/article/{id}")
    public String editArticle(@PathVariable("id") String id, HttpServletRequest request){
        Article article = articleService.selectArticleById(Integer.valueOf(id));
        request.setAttribute("article",article);
        request.setAttribute("categories",article.getCategories());
        return "back/article_edit";
    }

    //  文章修改处理
      @PostMapping(value = "/article/modify")
      @ResponseBody
    public ArticleResponseData modifyArticle(Article article){
        try {
            articleService.updateArticleWithId(article);
            log.info("文章更新成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            log.error("文章更新失败，错误信息: "+e.getMessage());
            return ArticleResponseData.fail();
        }
    }

    // 文章删除
    @PostMapping(value = "/article/delete")
    @ResponseBody
    public ArticleResponseData delete(@RequestParam int id)
    {
        try {
            articleService.deleteArticleWithId(id);
            log.info("文章删除成功");
            return ArticleResponseData.ok();
        } catch (Exception e) {
            log.error("文章删除失败，错误信息: "+e.getMessage());
            return ArticleResponseData.fail();
        }
    }
}

